﻿using System.Collections.Generic;
using System.Linq;
using Coder.Workflow;
using Identity;

namespace Fee.Orders
{
   
    public class DelegateOrderBase : ProcessInstance
    {
        private IList<DelegateOrderDetail> _details;

        public DelegateOrderBase(WorkProcess wrWorkProcess, User user) : base(wrWorkProcess, user)
        {
        }

        protected DelegateOrderBase()
        {
        }

        public string ThirdPartyWorkflowId { get; set; }

        /// <summary>
        /// </summary>
        public IList<DelegateOrderDetail> Details
        {
            get => _details ?? (_details = new List<DelegateOrderDetail>());
            set => _details = value;
        }

        public ConstructionTask ConstructionTask { get; set; }
        public string BatchNo { get; set; }

        public void Add(IEnumerable<DelegateOrderDetail> details)
        {
            var dict = new Dictionary<string, DelegateOrderDetail>();
            foreach (var item in details)
            {
                var key = item.ContractType + item.ConstructionType.ToString();
                if (!dict.ContainsKey(key))
                {
                    dict.Add(key, item);
                    Details.Add(item);
                }
            }
        }


        public void UpdateBy(IEnumerable<DelegateOrderDetail> details)
        {
            var updateDate = details.ToDictionary(
                f => f.ContractType + "" + f.ConstructionType,
                f => f
            );

            foreach (var item in Details)
            {
                var key = item.ContractType + item.ConstructionType.ToString();
                if (updateDate.ContainsKey(key))
                    item.UpdateBy(updateDate[key]);
            }
        }
    }
}